package com.qxl.forest.business.service.Implement;

import com.qxl.forest.business.service.RoleService;
import com.qxl.forest.business.persistence.dao.RelationDao;
import com.qxl.forest.business.persistence.dao.RoleDao;
import com.qxl.forest.business.persistence.entity.Relation;
import com.qxl.forest.util.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: qiuxinlin
 * @Dercription:
 * @Date: 15:42 2017/12/7
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RelationDao relationDao;
    @Autowired
    RoleDao roleDao;

    @Override
    @Transactional
    public void setAuthority(Integer roleId, String menuIds) {
        //删除该角色的所有权限
        relationDao.deleteByRoleid(roleId);
        //添加新的权限
        for (Integer id : Convert.toIntArray(menuIds)) {
            Relation relation = new Relation();
            relation.setRoleid(roleId);
            relation.setMenuid(id);
            relationDao.save(relation);
        }
    }

    @Override
    @Transactional
    public void delRoleById(Integer roleId) {
        //删除角色
        roleDao.delete(roleId);
        // 删除该角色所有的权限
        relationDao.deleteByRoleid(roleId);
    }
}
